package com.example.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

//这是类注解
@RequestMapping("/request")
@RestController
public class RequestController {
    //这是方法注解
    @RequestMapping("/r1")
    public String r1(String keyword){
        return "接受参数:"+keyword;
    }
    @RequestMapping("/r2")
    public String r2(String userName,String password){
        return "接受参数:userName:"+userName+",password:"+password;
    }
    @RequestMapping("/r3")
    public String r3(Integer number){
        return "接收参数:"+number;
    }
    @RequestMapping("/r4")
    public String r4(int number){
        return "接收参数:"+number;
    }
    @RequestMapping("/r5")
    //传递一个对象
    public String r5(UserInfo userInfo){
        return "接收参数:useInfo="+userInfo.toString();
    }
    @RequestMapping("/r6")
    //从前端接受参数q，赋值给keyword
    public String r6(@RequestParam(value = "q",required = false)String keyword){
        return "接收参数:keyword="+keyword;

    }
    @RequestMapping("/r7")
    //从前端接受参数q，赋值给keyword
    public String r7(@RequestParam(value = "q")String keyword){
        return "接收参数:keyword="+keyword;
    }
    //传递数组
    @RequestMapping("/r8")
    public String r8(String[] arr){
        return "接收参数:arr="+ Arrays.toString(arr);
    }
    //传递集合
    @RequestMapping("/r9")
    public String r9(@RequestParam List<Integer> list){
        return "接受参数:list="+list;
    }
    @RequestMapping("/r10")
    //传递json对象
    public String r10(@RequestBody UserInfo userInfo){
        return userInfo.toString();
    }
    //从URL中获取参数
    @RequestMapping("/article/{articleid}")
    public String r11(@PathVariable Integer articleid){
        return "获取文章ID:"+articleid;
    }
    @RequestMapping("/article/{type}/{articleid}")
    public String r12(@PathVariable Integer articleid,@PathVariable("type")
    String articeleType){
        return "获取文章ID"+articleid+",type:"+articeleType;
    }
    //上传文件
    @RequestMapping("/r13")
    public String r13(@RequestPart("file") MultipartFile multipartFile) throws IOException {
        //获取文件名称
        String fileName=multipartFile.getOriginalFilename();
        //文件上传到指定路径
        multipartFile.transferTo(new File("D:/maybe/"+multipartFile.getOriginalFilename()));
        return "接收到文件名称为:"+fileName;
    }
    @RequestMapping("/r14")
    public String r14(HttpServletRequest request){
        Cookie[] cookies=request.getCookies();
        if(cookies!=null){
            for(Cookie cookie:cookies){
                System.out.println(cookie.getName()+":"+cookie.getValue());
            }
        }
        return "返回Cookie成功";
    }
    @RequestMapping("/r15")
    public String r15(@CookieValue("java")String java){
        return "从Cookie中获取java的值:"+java;
    }
    //存储session
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        //从Cookie中获取sessionId，根据sessionId获取Session对象
        //默认为true，如果没有则返回一个空对象
        //如果为false，如果为空，则返回null
        HttpSession session=request.getSession();
        session.setAttribute("userName","Maybe");
        session.setAttribute("age","16");
        return "设置session成功";
    }
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session=request.getSession(false);
        if(session==null){
            return "用户未登录";
        }else{
            String userName=(String) session.getAttribute("userName");
            return "登录用户为:"+userName;
        }
    }
    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session){
        String userName=(String) session.getAttribute("userName");
        return "登录用户为:"+userName;
    }
    @RequestMapping("/getSession3")
    public String getSession2(@SessionAttribute("userName") String userName){
        return "登录用户为:"+userName;
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String hostname=request.getHeader("Host");
        return "从header中获取Host:"+hostname;
    }
    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent")String userAgent){
        return "从header中获取userAgent:"+userAgent;
    }





}
